<?php

namespace App\Models;

use Cache;
use Illuminate\Database\Eloquent\Model;

class Goods extends Model
{
    public function getpluck($cate = '女装/女士精品', $page = 0, $pageSize = 10)
    {
        $res = $this->where('status', 1)->offset($page * $pageSize)->limit($pageSize)->orderBy('sort', 'DESC')->orderBy('view', 'DESC')
            ->orderBy('sales_month', 'DESC')->orderBy('id', 'DESC')
            ->where('category', $cate)->get();
        return $res;
    }

    public function getGoodsRand($num = 1)
    {
        $ids = getShuffleRand($this->where('status', 1)->pluck('id')->toArray(), $num);
        $res = $this->whereIn('id', $ids)->get();
        return $res;
    }

    public function getGoodsTpwd($num = 1)
    {
        $ids = getShuffleRand($this->where('status', 1)->pluck('id')->toArray(), $num);
        $res = $this->whereIn('id', $ids)->get();
        return $res;
    }

    public function getCategoryArray()
    {
        $cacheName = md5('getCategoryArray00');
        if (!Cache::has($cacheName)) {
            $res = Goods::where('status', 1)->select('category')->groupBy('category')->get();
            foreach ($res as $key => $obj) {
                $obj->category = str_replace('/', '-', $obj->category);
            }
            $res = $res->toArray();
            Cache::forever($cacheName, $res);
        }
        $res            = Cache::get($cacheName);
        $data['list1']  = array_chunk($res, 1);
        $data['list2']  = array_chunk($res, 2);
        $data['list3']  = array_chunk($res, 3);
        $data['list4']  = array_chunk($res, 4);
        $data['list5']  = array_chunk($res, 5);
        $data['list6']  = array_chunk($res, 6);
        $data['list7']  = array_chunk($res, 7);
        $data['list8']  = array_chunk($res, 8);
        $data['list9']  = array_chunk($res, 9);
        $data['list10'] = array_chunk($res, 10);
        return $data;
    }

    public function getGoodsData($cate = 0)
    {
        $cacheName = md5("getGoodsData");
        $condition = array('category'=>$cate);
        if (!Cache::has($cacheName)) {
            if($cate === 0 ){
                $ids = getShuffleRand($this->where('status', 1)->pluck('id')->toArray(), 150);
                $res = $this->whereIn('id', $ids)->select('id','gid', 'title', 'coupons_amount', 'cover as path', 'generalize_link')
                    ->orderBy('sort', 'DESC')->orderBy('view', 'DESC')->orderBy('sales_month', 'DESC')->orderBy('id', 'DESC')->get();
            }else{
                $ids = getShuffleRand($this->where('status', 1)->where($condition)->pluck('id')->toArray(), 150);
                $res = $this->where($condition)->whereIn('id', $ids)
                    ->select('id','gid', 'title', 'coupons_amount', 'cover as path', 'generalize_link')
                    ->orderBy('sort', 'DESC')->orderBy('view', 'DESC')->orderBy('sales_month', 'DESC')->orderBy('id', 'DESC')->get();
            }

            $res = $res->toArray();
            Cache::forever($cacheName, $res);
        }
        $res            = Cache::get($cacheName);
        $data['list1']  = array_chunk($res, 1);
        $data['list2']  = array_chunk($res, 2);
        $data['list3']  = array_chunk($res, 3);
        $data['list4']  = array_chunk($res, 4);
        $data['list5']  = array_chunk($res, 5);
        $data['list6']  = array_chunk($res, 6);
        $data['list7']  = array_chunk($res, 7);
        $data['list8']  = array_chunk($res, 8);
        $data['list9']  = array_chunk($res, 9);
        $data['list10'] = array_chunk($res, 10);
        return $data;
    }

    public function getClientGoods($gids)
    {
        $res = $this->where('status', 1)->whereIn('gid', $gids)->select('id','gid', 'title', 'coupons_amount', 'cover as path', 'generalize_link')
            ->orderBy('sort', 'DESC')->orderBy('view', 'DESC')->orderBy('sales_month', 'DESC')->orderBy('id', 'DESC')->get();
        $res = $res->toArray();
        return $res;
    }
}
